package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.DingtalkUserAttendanceResultDao;
import cn.topevery.garden.entity.po.DingtalkUserAttendanceResult;
import cn.topevery.garden.entity.query.DingtalkUserAttendanceResultQuery;
import cn.topevery.garden.entity.vo.DingtalkUserAttendanceResultVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author code-generate
 * @date 2021-09-24
 */
@Repository
public class DingtalkUserAttendanceResultDaoImpl extends BaseDaoImpl<DingtalkUserAttendanceResult> implements DingtalkUserAttendanceResultDao {
    @Override
    public <P extends DbCsp> List<DingtalkUserAttendanceResult> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<DingtalkUserAttendanceResultVO> listPage(DingtalkUserAttendanceResultQuery query) {
        String table = "t_dingtalk_user_attendance_result tduar \n" +
                "join t_dingtalk_user_relation tdur on tduar.c_user_id=tdur.c_user_id\n" +
                "join t_sys_user tsu on tsu.c_id=tdur.c_pmi_user_id";

        String columns = "tduar.*,tsu.c_user_name";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tduar.c_user_id desc");
        sqlQueryMate
                .where(true, "tduar.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.beginDate), "tduar.c_work_date", ScriptConditionEnum.GreaterThan, query.beginDate)
                .where(StringUtils.isNotBlank(query.endDate), "tduar.c_work_date", ScriptConditionEnum.LessThan, query.endDate)
                .where(StringUtils.isNotBlank(query.getName()), "tsu.c_user_name", ScriptConditionEnum.Like, query.getName())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, DingtalkUserAttendanceResultVO.class);
    }

    @Override
    public DingtalkUserAttendanceResult getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public LocalDate getMaxDate() {
        String table = "t_dingtalk_user_attendance_result tduar";

        String columns = "max(c_work_date) date";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tduar.c_db_status", ScriptConditionEnum.Eq, 0)
        ;
        Map<String, Object> result = getOneMap(sqlQueryMate);
        return (result != null && result.get("date") != null) ? ((Date) result.get("date")).toLocalDate() : null;
    }

}
